package com.eqdd.yiqidian.utils;

import com.eqdd.yiqidian.widget.WaveSideBarView;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by lv on 17-8-24.
 */

public class SideBarUtil {
    public static void updateData(WaveSideBarView sideBarView, List datas, String field) {

            if (datas != null) {
                List mLetters = new ArrayList<String>();
                HashMap<String, Integer> letterPositionMap = new HashMap<>();
                for (int i = 0; i < datas.size(); i++) {
                    String headPinyin = null;
                    try {
                        headPinyin = getHeadField(datas.get(i), field);
                    } catch (Exception e) {
                        continue;
                    }
                    if (mLetters.size() == 0) {
                        mLetters.add(headPinyin);
                        letterPositionMap.put(headPinyin, i);
                    } else {
                        if (!headPinyin.equals(mLetters.get(mLetters.size() - 1))) {
                            mLetters.add(headPinyin);
                            letterPositionMap.put(headPinyin, i);
                        }
                    }
                }
            sideBarView.updateDate(letterPositionMap,mLetters);
            }


    }

    private static String getHeadField(Object o, String field) throws Exception {
        Field declaredField = o.getClass().getDeclaredField(field);
        declaredField.setAccessible(true);
        return PinYinUtil.head((String) declaredField.get(o));
    }
}
